#include <stdlib.h>
#include <stdio.h>

int tigermain();

long long *_initArray_(long long size, long long init) {
	long long *a = (long long *)malloc(size * sizeof(long long));
	for(long long i = 0; i < size; ++i) 
		a[i] = init;
	return a;
}

long long *_allocRecord_(long long size) {
	long long *a = (long long *)malloc(size * sizeof(long long));
	for(long long i = 0; i < size; ++i) 
		a[i] = 0;
	return a;
}

struct string {
	int length;
	unsigned char chars[1];
};

int _stringEqual_(struct string *s, struct string *t) {
	// if the content of 's' is equal to 't', then return 0
	// otherwise return 1
	if(s == t) 
		return 0;
	if(s->length != t->length) 
		return 1;
	for(int i=0; i < s->length; ++i) {
		if(s->chars[i] != t->chars[i])
			return 1;
	}
 	return 0;
}

void _print_(struct string *s) {
	unsigned char *p = s->chars;
	for(int i = 0; i < s->length; i++) 
		putchar(*p++);
}

void _flush_() {
	fflush(stdout);
}

struct string consts[256];
struct string empty = {0,""};

int main() {
	for(int i = 0; i < 256; ++i) {
		consts[i].length = 1;
		consts[i].chars[0] = i;
	}
 return tigermain();
}

int _ord_(struct string *s) {
	if(s->length == 0)
		return -1;
	else 
		return s->chars[0];
}

struct string *_chr_(int i) {
	if(i < 0 || i >= 256) {
		printf("chr(%d) out of range\n",i); 
		exit(1);
	}
	return consts + i;
}

int _size_(struct string *s) {
	return s->length;
}

struct string *_substring_(struct string *s, int first, int n) {
	if(first < 0 || n < 0 || first + n > s->length) {
		printf("substring([%d],%d,%d) out of range\n", s->length, first, n);
		exit(1);
	}
	if(!n)
		return &empty;
	if(n == 1) 
		return consts+s->chars[first];
	struct string *t = (struct string *)malloc(sizeof(int) + n);
	t->length = n;
	for(int i = 0; i < n; ++i) 
		t->chars[i] = s->chars[first+i];
	return t;
}

struct string *_concat_(struct string *a, struct string *b) {
	if(!a->length) 
		return b;
	if(!b->length) 
		return a;
	int n = a->length + b->length;
	struct string *t = (struct string *)malloc(sizeof(int) + n);
	t->length=n;
	for(int i = 0; i < a->length; ++i)
		t->chars[i]=a->chars[i];
	for(int i=0; i < b->length; ++i)
		t->chars[i+a->length]=b->chars[i];
	return t;
}

int _not_(int i) {
	return !i;
}

void _exit_(int status) {
	exit(status);
}

struct string *_getchar_() {
	int i = getc(stdin);
	if(i == EOF) 
		return &empty;
	else
		return consts + i;
}
